//
//  GLBadgeViewController.h
//  GLFormKitDemo
//
//  Created by 121 on 2018/3/28.
//  Copyright © 2018年 杭州吉利控股信息中心. All rights reserved.
//

#import "GLFormFollowCell.h"
#import <Masonry/Masonry.h>

//d
@interface GLFormFollowCell()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *recordLabel;
@property (nonatomic, strong) UIButton *buildFollowBtn;
@end

@implementation GLFormFollowCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    
    //最后一行的"上次记录"
    UILabel *thirdLeftLabel = [[UILabel alloc] init];
    thirdLeftLabel.font = [UIFont systemFontOfSize:16];
    thirdLeftLabel.textColor = [UIColor grayColor];
    thirdLeftLabel.text = @"上次记录";
    self.titleLabel = thirdLeftLabel;
    [self.contentView addSubview:thirdLeftLabel];
    
    UILabel *recordLabel = [[UILabel alloc] init];
    recordLabel.font = [UIFont systemFontOfSize:16];
    recordLabel.textColor = [UIColor blackColor];
    recordLabel.numberOfLines = 0;
    recordLabel.text = @"";
    self.recordLabel = recordLabel;
    [self.contentView addSubview:recordLabel];
    
    UIButton *buildFollowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [buildFollowBtn setTitle:@"新建跟进" forState:UIControlStateNormal];
    buildFollowBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [buildFollowBtn setImage:[UIImage imageNamed:@"icon-add"] forState:UIControlStateNormal];
    CGFloat gap = 4;
    buildFollowBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -gap / 2, 0, gap / 2);
    buildFollowBtn.titleEdgeInsets = UIEdgeInsetsMake(0, gap / 2, 0 , - gap / 2);
    buildFollowBtn.contentEdgeInsets = UIEdgeInsetsMake(0, gap / 2, 0, gap / 2);    [buildFollowBtn addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside];
    [buildFollowBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    buildFollowBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [buildFollowBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.buildFollowBtn = buildFollowBtn;
    [self.contentView addSubview:buildFollowBtn];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [self layoutUI];
}
- (void)layoutUI{
    
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(self.edgeMargin);
        make.height.mas_equalTo(24);
        make.top.equalTo(self.contentView).offset(10);

    }];
    
    [self.recordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right).offset(self.contentMargin);
        make.top.equalTo(self.contentView).offset(14);
        make.right.equalTo(self.contentView).offset(-self.contentMargin);
    }];
    
    [self.buildFollowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.recordLabel.mas_bottom);
        make.right.equalTo(self.contentView).offset(-self.contentMargin);
        
    }];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self layoutUI];
}


- (void)clickEvent:(UIButton *)btn
{
    [self clicked:btn subTag:@"btnClick"];
}


+ (CGFloat)cellHeightByText:(NSString*)text hideButton:(BOOL)yesOrNot{
    CGFloat cellHeight = 0;
    if (text && ![text isEqualToString:@""]) {

        NSMutableDictionary *attrsDict = [NSMutableDictionary dictionary];
        attrsDict[NSFontAttributeName] = [UIFont systemFontOfSize:16];
        CGFloat currWidth = [UIScreen mainScreen].bounds.size.width - 89;
        if(YES){
            currWidth-=20;
        }
        CGSize recordSize = [text boundingRectWithSize:CGSizeMake(currWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrsDict context:nil].size;
        //  LGLog(@"%f--%f===%f",width,recordSize.height,recordSize.width);
        cellHeight = recordSize.height;
    }
    
    cellHeight+=14;//top
    if (!yesOrNot) {
        cellHeight+=25;//button
    }
    cellHeight+=4;//bottom
    return cellHeight>44.0?cellHeight:44.0;
}

#pragma mark- getter and setter

-(void)setTitle:(NSString *)title{
    self.titleLabel.text = title;
}
-(void)setRecord:(NSString *)record{
    self.recordLabel.text = record;
}
-(void)setButtonImage:(UIImage *)buttonImage{
    [self.buildFollowBtn setImage:buttonImage forState:UIControlStateNormal];
    [self.buildFollowBtn setImage:buttonImage forState:UIControlStateHighlighted];
}
-(void)setButtonTitle:(NSString *)buttonTitle{
    [self.buildFollowBtn setTitle:buttonTitle forState:UIControlStateNormal];
}
-(void)setButtonTitleColor:(UIColor *)buttonTitleColor{
    [self.buildFollowBtn setTitleColor:buttonTitleColor forState:UIControlStateNormal];
}

-(void)setButtonHide:(BOOL)buttonHide{
    _buttonHide = buttonHide;
    self.buildFollowBtn.hidden = _buttonHide;
}



@end
